/// Flutter code sample for TabBar

// [TabBar] can also be implmented by using a [TabController] which provides more options
// to control the behavior of the [TabBar] and [TabBarView]. This can be used instead of
// a [DefaultTabController], demonstrated below.

import 'package:flutter/material.dart';

import '19_my_tab_bar.dart';

void main() => runApp(const MyApp());

/// This is the main application widget.
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

/// This is the private State class that goes with MyStatefulWidget.
/// AnimationControllers can be created with `vsync: this` because of TickerProviderStateMixin.
class _MyStatefulWidgetState extends State<MyStatefulWidget>
    with TickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 3, vsync: this);
    _tabController.addListener(() {
      // 滑动只会回调一次，但是点击会回调两次，所以加上判断
      if(_tabController.index == _tabController.animation!.value) {
        print('you clicked ${_tabController.index}');
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // 也可以使用 TabController，这样就不用DefaultTabController包裹
    return Scaffold(
      appBar: MyTabBar(controller: _tabController),
      // appBar: AppBar(
      //   title: const Text('TabBar Widget'),
      //   bottom: MyTabBar(controller: _tabController),
        // bottom: TabBar(
        //   indicatorSize: TabBarIndicatorSize.label,
        //   indicator: BoxDecoration(
        //     color: Colors.black12,
        //     border: Border.all(color: Colors.grey)
        //   ),
        //   // labelStyle: TextStyle(color: Colors.black),
        //   controller: _tabController,
        //   tabs: const <Widget>[
        //     Tab(
        //       // icon: Icon(Icons.cloud_outlined),
        //       text: '第一页',
        //     ),
        //     Tab(
        //       // icon: Icon(Icons.beach_access_sharp),
        //       text: '第二页',
        //     ),
        //     Tab(
        //       // icon: Icon(Icons.brightness_5_sharp),
        //       text: '第三页',
        //     ),
        //   ],
        // ),
      // ),
      body: TabBarView(
        controller: _tabController,
        children: <Widget>[
          Center(
            child: Page1(),
          ),
          Center(
            child: Text('It\'s rainy here'),
          ),
          Center(
            child: Text('It\'s sunny here'),
          ),
        ],
      ),
    );
  }
}


class Page1 extends StatefulWidget {
  @override
  _Page1State createState() => _Page1State();
}

class _Page1State extends State<Page1> with AutomaticKeepAliveClientMixin {

  // 会被多次初始化，所以需要做一点操作
  // 1.混入AutomaticKeepAliveClientMixin
  // 2.重写wantKeepAlive,返回true
  // 3.调用父类build函数
  @override
  void initState() {
    super.initState();
    debugPrint('init');
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      child: Text('这是第一页'),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
